//
//  SearchHeadView.m
//  Topit
//
//  Created by dllo on 15/12/15.
//  Copyright © 2015年 YXY. All rights reserved.
//

#import "SearchHeadView.h"
#import "YXYWallImageCollectionViewCell.h"
#import "YXYSelectionViewController.h"
@implementation SearchHeadView

- (void)dealloc {
    [_flowLayout release];
    [_collectionView release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
        
        
        self.flowLayout.minimumInteritemSpacing = 0;
        self.flowLayout.minimumLineSpacing = 0;
        
        self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:self.flowLayout];
        
        self.collectionView.backgroundColor = [UIColor whiteColor];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        self.collectionView.scrollEnabled = NO;
        [self.collectionView registerClass:[YXYWallImageCollectionViewCell class] forCellWithReuseIdentifier:@"YXYWallImageCollectionViewCellIdentifier"];
        [self.collectionView nightMode];
        [self addSubview:self.collectionView];
        [_flowLayout release];
        [_collectionView release];
    }
    return self;
}

- (void)setDataSource:(NSMutableArray *)dataSource {
    if (_dataSource != dataSource) {
        [_dataSource release];
        _dataSource = [dataSource retain];
    }
    [self.collectionView nightMode];
    [self.collectionView reloadData];
    self.flowLayout.itemSize = CGSizeMake(self.width / 2, self.height / (dataSource.count / 2));
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    [super applyLayoutAttributes:layoutAttributes];

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    YXYWallImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YXYWallImageCollectionViewCellIdentifier" forIndexPath:indexPath];
    YXYWallItemModel *wallItemModel = _dataSource[indexPath.item];
    cell.name.text = wallItemModel.name;
    cell.bio.text = wallItemModel.bio;
    [cell.images sd_setImageWithURL:wallItemModel.icon[@"url"]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    YXYWallItemModel *wallItemModel = _dataSource[indexPath.item];

    if (indexPath.item > 1) {
        [self.delegate searchReadSkipDelegate:wallItemModel.next];
    }else {
        [self.delegate searchSkipDelegate:wallItemModel.next];
    }
}

@end
